from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.support.ui import WebDriverWait
import pytest,time
class Config():
    # 初始化driver
    def __init__(self,driver):
        self.driver=driver
    # 获取地址
    def get(self,url):
        try:
            self.driver.get(url)
        except Exception as e:
            print(e)

    def find_element(self,loc):
        try:
            # 显示等待
            WebDriverWait(self.driver,20,0.5).until(lambda driver:self.driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            # 时间戳
            t=time.strftime('%Y%m%d-%H%M%S',time.localtime())
            # 截图
            self.driver.save_screenshot(t+'.png')
            print(e)
    # 点击
    def click(self,loc):
        try:
            self.find_element(loc).click()
        except Exception as e:
            print(e)
    # 输入
    def send_keys(self,loc,info):
        try:
            self.find_element(loc).send_keys(info)
        except Exception as e:
            print(e)
    # 文本断言
    def text(self,loc):
        try:
            return self.driver.find_element(*loc).text
        except Exception as e:
            print(e)
    # 键盘回车
    def key_enter(self,loc):
        self.driver.find_element(loc,Keys.ENTER)
    # 定位多个元素
    def find_elements(self,loc):
        try:
            return self.driver.find_elements(*loc)
        except Exception as e:
            print(e)
    # 是否可见
    def is_displayed(self,loc):
        return self.driver.find_element(*loc).is_displayed()
    # 滚动
    def execute_script(self,loc):
        self.driver.execute_script(f'window.scrollTo(0,{loc})')


